/**
 * 
 */
package cii_functions;

import javax.swing.JComponent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cii_lib.CCII_ACCESS_INTERF;
import cii_lib.CIIdata;
import cii_lib_graph.GCII_BITSGroup;
import cii_lib_graph.GInterfaceTerminal;

import componentEnhancement.CiiDataContainer;

/**
 * @author pdrabik
 *
 */
public class FCII_interfaceReader implements Runnable {
	
	private static final Logger log = LoggerFactory
			.getLogger(FCII_interfaceReader.class);

	private CCII_ACCESS_INTERF register = null;
	private CIIdata res = null;
	private long interval = 300;
	//FIXME TEMP!
	private long exactVal=100;
	private GCII_BITSGroup bg;
	//
	private GInterfaceTerminal terminal = new GInterfaceTerminal();
	private JComponent field ;
	private CiiDataContainer containerData;
	
	public FCII_interfaceReader() {
		
	}
	
	//FIXME TEST !?
	public void setExactVal(long val) {
		exactVal = val;
	}
	public void setTempBitsGr(GCII_BITSGroup bg) {
		this.bg = bg;
	}
	
	public FCII_interfaceReader(CCII_ACCESS_INTERF reg) {
		register = reg;
		res = new CIIdata(reg.Width());
		terminal = new GInterfaceTerminal(reg);
	}
	public FCII_interfaceReader(GInterfaceTerminal term) {
		register = term.getInterface();
		res = new CIIdata(register.Width());
		terminal = term;
	}
	/**
	 * 
	 * @param term GInterfaceTerminal instance
	 * @param time Read delay in miliseconds
	 */
	public FCII_interfaceReader(GInterfaceTerminal term, long time) {
		register = term.getInterface();
		interval=time;
		res = new CIIdata(register.Width());
		terminal = term;
	}
	
	//TODO nie uzywany
//	public FCII_interfaceReader(GInterfaceTerminal term, JComponent gui,long time) {
//		register = term.getInterface();
//		interval=time;
//		res = new CIIdata(register.Width());
//		terminal = term;
//		field = gui;
//	}
	
	public FCII_interfaceReader(GInterfaceTerminal term, CiiDataContainer dataPlace,long time) {
		register = term.getInterface();
		interval=time;
		res = new CIIdata(register.Width());
		terminal = term;
		containerData = dataPlace;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		for (;;) {
		register.Read(0, 0, res);
		terminal.getReadTextField().setText(""+res.GetValue());
		log.debug("thread reader got: " + res.GetBin0()+"\t"+res.GetValue());
		if (containerData!=null) {
			long value =res.GetValue();
			log.debug(Long.toString(value/exactVal));
			int val = (int)((100*value)/exactVal);
			if (value>=exactVal) {
			containerData.setValue(100);
			containerData.setString("100 %");
			terminal.getReadTextField().setText(""+exactVal);
			bg.getBitsButtons().get(1).setText("1");
			bg.getBitsButtons().get(0).setSelected(false);
			bg.getBitsButtons().get(0).setText("0");
//			break;
			}
			
			containerData.setValue(val);
			containerData.setString(val+" %");
			}
		try {
			java.lang.Thread.sleep(interval);
		} catch (InterruptedException e) {
			log.debug("Reader stopped from: "+ e.getLocalizedMessage());
			break;
		}
		} //for
	}

	/**
	 * @return the interval
	 */
	public long getInterval() {
		return interval;
	}

	/**
	 * @param interval the interval to set
	 */
	public void setInterval(long interval) {
		this.interval = interval;
	}

	/**
	 * @return the register
	 */
	public CCII_ACCESS_INTERF getRegister() {
		return register;
	}

	/**
	 * @param register the register to set
	 */
	public void setRegister(CCII_ACCESS_INTERF register) {
		this.register = register;
	}

	/**
	 * @return the result
	 */
	public CIIdata getRes() {
		return res;
	}

	/**
	 * @param res the result to set
	 */
	public void setRes(CIIdata res) {
		this.res = res;
	}

	/**
	 * @return the terminal
	 */
	public GInterfaceTerminal getTerminal() {
		return terminal;
	}

	/**
	 * @param terminal the terminal to set
	 */
	public void setTerminal(GInterfaceTerminal terminal) {
		this.terminal = terminal;
	}

}
